package com.cwd.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.cwd.constant.MessageConstant;
import com.cwd.dao.MemberDao;
import com.cwd.dao.OrderDao;
import com.cwd.dao.OrderSettingDao;
import com.cwd.entity.Result;
import com.cwd.pojo.Member;
import com.cwd.pojo.Order;
import com.cwd.pojo.OrderSetting;
import com.cwd.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.Map;

/**
 * @PackageName: com.cwd.service
 * @ClassName: OrderServiceImpl
 * @Author: CWD
 * @Date: 2020/3/11 17:44
 * @Description:
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    //提交预约
    @Override
    public Result order(Map map) throws Exception {
        //检查用户所选择的的预约日期是否设置了预约，如果没有设置就不能进行预约
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        if (orderSetting == null) {
            //说明没有设置预约，直接reture
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //检查用户所选择的预约日期是否已经约满
        int number = orderSetting.getNumber(); //可预约人数
        int reservations = orderSetting.getReservations(); //已预约人数
        if (reservations >= number) {
            //已预约满，无法预约，直接return
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //检查用户是否重复预约（同一天预约同一个套餐）
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);//根据手机号查询用户是否存在，如果不存在肯定没预约，如果存在再判断是否重复预约
        if(member != null){
            //判断是否重复预约
            Integer memberId = member.getId();
            Integer setmealId = Integer.parseInt((String) map.get("setmealId"));
            Order order = new Order(memberId,date,setmealId);
            Integer count = orderDao.findByCondition(order);
            if (count > 0) {
                //count>0说明已预约，不可重复预约,直接return
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }else {
            //不是会员先添加为会员再继续预约
            member = new Member();
            member.setName((String) map.get("name"));
            member.setIdCard((String) map.get("idCard"));
            member.setPhoneNumber(telephone);
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);    //会员注册
        }
        //预约成功，更新预约人数
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(date);
        order.setOrderType((String) map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);
        //设置预约人数
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }
    //根据预约id查询预约相关信息，用于界面展示
    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if (map != null) {
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
